<?php

namespace app\model;

use think\helper\Str;
use think\Model;
use think\model\relation\HasOne;

class StationPriceTask extends Model
{
    public function saveData()
    {
        $param = request()->post();
        $account = request()->account;
        $code = date("YmdHis") . Str::random(3, 1);
        if (!is_numeric($param['price'])) throw new \Exception("请填写改动数值");
        //换行符分割
        $station_id = explode("\n", $param['station_id']);
        $station_id = array_values(array_filter($station_id));
        $sid = app(StationBasic::class)->where('station_id', 'in', $station_id)->column("id","station_id");
        unset($param['station_id']);
        $supplier = $param['sup_id'];
        unset($param['sup_id']);
        $result = array_map(function ($item) use ($code, $param, $account,$sid) {
            return array_merge($param, ['station_id' => $item, 'task_id' => $code, 'state' => 1, 'admin' => $account['name'] ?? '系统任务','sid'=>$sid[$item] ?? null]);
        }, $station_id);
        if (!empty($supplier)) {
            $res = [];
            foreach ($supplier as $v) {
                $r = array_map(function ($item) use ($v) {
                    return array_merge($item, ['sup_id' => $v]);
                }, $result);
                $res = array_merge($res, $r);
            }
            $result = $res;
        }
        return $this->saveAll($result);
    }

    public function lst(): array
    {
        $param = request()->param();
        $data = $this->with(['stations', 'supplier','oil'])->when(!empty($param['task_time']), function ($query) use ($param) {
            $query->where('task_time', 'between', [$param['task_time'][0] . " 00:00:00", $param['task_time'][1] . " 23:59:59"]);
        })->when(!empty($param['task_id']), function ($query) use ($param) {
            $query->where('task_id', $param['task_id']);
        })->when(!empty($param['type']), function ($query) use ($param) {
            $query->where('type', $param['type']);
        })->when(!empty($param['sup_id']), function ($query) use ($param) {
            $query->where('sup_id', $param['sup_id']);
        })->when(!empty($param['station_id']), function ($query) use ($param) {
            $station_id = StationBasic::whereRaw("station_id = '$param[station_id]' OR name like '%$param[station_id]%'")->column("station_id");
            $query->where('station_id', 'in', $station_id);
        })->when(!empty($param['price_type']),function ($query) use ($param){
            $query->where('price_type',$param['price_type']);
        })->when(!empty($param['price_rule_id']),function ($query) use ($param){
            $query->where('price_rule_id',$param['price_rule_id']);
        })->when(!empty($param['state']),function ($query) use ($param){
            $query->where('state',$param['state']);
        })->order(['task_id' => 'asc', 'create_time' => 'desc'])->paginate(['list_rows' => 30, 'page' => $param['page'] ?? 1])->toArray();
        return pageData($data);
    }

    public function stations(): HasOne
    {
        return $this->hasOne(StationBasic::class, 'station_id', 'station_id');
    }

    public function supplier(): HasOne
    {
        return $this->hasOne(Supplier::class, 'id', 'sup_id');
    }

    public function oil(): HasOne
    {
        return $this->hasOne(StationOilNumber::class, 'id', 'oil_id');
    }
}
